﻿using Memcached.ClientLibrary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace EmoneyWeiXinCommon
{
    public class MemcachedObject : IMemcachedCache
    {

        static string cacheName = Guid.NewGuid().ToString();
        static MemcachedObject()
        {
            string servers = Config.MemCachedServerList;
            if (!string.IsNullOrEmpty(servers))
            {
                String[] serverlist = servers.Split('|');
                SockIOPool pool = SockIOPool.GetInstance(cacheName);
                pool.SocketConnectTimeout = 5000000;
                pool.SocketTimeout = 500000;
                pool.SetServers(serverlist);
                pool.Initialize();
            }
        }

        public MemcachedClient GetClient()
        {
            MemcachedClient mc = new MemcachedClient();
            mc.PoolName = cacheName;
            mc.EnableCompression = false;
            return mc;
        }


        public void Add(string key, object obj, int hours, int minutes, int seconds)
        {
            MemcachedClient cache = GetClient();

            if (!cache.Add(key, obj, DateTime.Now.Add(new TimeSpan(hours, minutes, seconds))))
            {
                var result = cache.Set(key, obj, DateTime.Now.Add(new TimeSpan(hours, minutes, seconds)));
            }

        }

        public void Remove(string key)
        {
            var c = GetClient();
            c.Delete(key);
            //throw new Exception();
        }

        public void FlushAll()
        {
            throw new Exception();
        }

        public T Get<T>(string key)
        {
            var c = GetClient();
            return (T)c.Get(key);
        }

        public string GetStr(string key)
        {
            var c = GetClient();
            return (string)c.Get(key);
        }

        /// <summary>
        /// 分割字符串
        /// </summary>
        public static string[] SplitString(string strContent, string strSplit)
        {
            if (!StrIsNullOrEmpty(strContent))
            {
                if (strContent.IndexOf(strSplit) < 0)
                    return new string[] { strContent };

                return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
            }
            else
                return new string[0] { };
        }

        /// <summary>
        /// 字段串是否为Null或为""(空)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StrIsNullOrEmpty(string str)
        {
            if (str == null || str.Trim() == string.Empty)
                return true;

            return false;
        }

        public Hashtable GetValues(string[] keys)
        {
            var client = GetClient();
            return client.GetMultiple(keys);

        }

    }
}
